<?php
 namespace app\common\upload;

 use app\common\service\WeginService;
 use EasyAdmin\upload\FileBase;
 use EasyAdmin\upload\driver\alioss\Oss;
 use EasyAdmin\upload\trigger\SaveDb;
 use think\facade\Filesystem;

 class AliossDriver extends FileBase
 {

    /**
     * 重写上传方法
     * @return array|void
     */
    public function save()
    {
        //把文件上传
        $this->completeFilePath = Filesystem::disk('public')->putFile('upload', $this->file);
         //实际保存的磁盘目录
        $completeFilePathReal = config('filesystem.disks.public.root')  . DIRECTORY_SEPARATOR .   $this->completeFilePath ;

        $this->completeFileUrl = ltrim( config('filesystem.disks.public.url'),'/')  . str_replace(DIRECTORY_SEPARATOR, '/', $this->completeFilePath);
        
        $upload = Oss::instance($this->uploadConfig)
        ->save($this->completeFileUrl, $completeFilePathReal);
        if ($upload['save'] == true) {
            SaveDb::trigger($this->tableName, [
                'uniacid'=> WeginService::getUniacid(),
                'upload_type'   => $this->uploadType,
                'original_name' => $this->file->getOriginalName(),
                'mime_type'     => $this->file->getOriginalMime(),
                'file_ext'      => strtolower($this->file->getOriginalExtension()),
                'url'           => $upload['url'],
                'create_time'   => time(),
            ]);
        }
        $this->rmLocalSave();
        return $upload;
    }
 }